//
//  ZZLCRegisterVC.m
//  ZZLC
//
//  Created by 杨春至 on 2017/12/14.
//  Copyright © 2017年 ZhongZeng. All rights reserved.
//

#import "ZZLForgetVC.h"
#import "ZZHelper.h"
#import "ZZLSetingPWDVC.h"
@interface ZZLForgetVC ()
{
    BOOL _timeStart;
}
@end

@implementation ZZLForgetVC

- (void)viewDidLoad {
        K_WS(weakSelf);
    [super viewDidLoad];
    self.title = @"忘记密码";
    self.phoneLabel.text = [NSString stringWithFormat:@"已发送验证码至%@,请稍等",self.phoneStr];
 
    [self.getcodeBtn addTarget:self action:@selector(clickBTNAction:) forControlEvents:UIControlEventTouchUpInside];
    [self clickBTNAction:self.getcodeBtn];
    [self.nextStepBtn bk_whenTapped:^{
        if (self.vadidateCodeTF.text.length) {
            [weakSelf validateNetworking];
        }
        else
        {
              [QMUITips showError:@"请输入短信验证码" inView:self.view hideAfterDelay:1.8];
        }
    }];
    // Do any additional setup after loading the view from its nib.
}
- (void)clickBTNAction:(UIButton *)sender
{
    [self getCodeNetWorking];
}
- (void)getCodeNetWorking
{
    [NetWorkingManager postWithRequestUrl:@"findPwdSendYzm" AndParameters:@{@"parameters":[@{@"username":self.phoneStr,@"type":@"2",@"imageCode":@"",@"phoneId":[ZZHelper deviceIPAdress],@"uCode":[ZZHelper ucode]} DataTOjsonStringAndAddVersion]} AndRequestBlock:^(NSDictionary *responseObject) {
        _timeStart = YES;
        NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerFireMethod:) userInfo:nil repeats:YES];
        [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
    } FailBlock:^(NSDictionary *errDic) {
        
    }];
}

- (void)validateNetworking
{
     K_WS(weakSelf);
    [NetWorkingManager postWithRequestUrl:@"findPwdYzmIsRight" AndParameters:@{@"parameters":[@{@"username":self.phoneStr,@"yzm":self.vadidateCodeTF.text,@"type":@"2",@"phoneId":[ZZHelper deviceIPAdress]} DataTOjsonStringAndAddVersion]} AndRequestBlock:^(NSDictionary *responseObject) {
        NSString *messageStr = responseObject[@"end"];
        if ([messageStr isEqualToString:@"ok"]) {
            ZZLSetingPWDVC *setingPwdVC = [[ZZLSetingPWDVC alloc]initWithNibName:@"ZZLSetingPWDVC" bundle:nil];
            setingPwdVC.phoneStr = self.phoneStr;
            setingPwdVC.fromWhere = FORGETVC;
            [weakSelf.navigationController pushViewController:setingPwdVC animated:YES];
            
        }
    } FailBlock:^(NSDictionary *errDic) {
        
    }];
}
- (void)timerFireMethod:(NSTimer *)theTimer
{
    NSCalendar *cal = [NSCalendar currentCalendar];//定义一个NSCalendar对象
    NSDateComponents *endTime = [[NSDateComponents alloc] init];    //初始化目标时间...
    NSDate *today = [NSDate date];    //得到当前时间
    
    NSDate *date = [NSDate dateWithTimeInterval:60 sinceDate:today];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *dateString = [dateFormatter stringFromDate:date];
    
    static int year;
    static int month;
    static int day;
    static int hour;
    static int minute;
    static int second;
    if(_timeStart) {//从NSDate中取出年月日，时分秒，但是只能取一次
        year = [[dateString substringWithRange:NSMakeRange(0, 4)] intValue];
        month = [[dateString substringWithRange:NSMakeRange(5, 2)] intValue];
        day = [[dateString substringWithRange:NSMakeRange(8, 2)] intValue];
        hour = [[dateString substringWithRange:NSMakeRange(11, 2)] intValue];
        minute = [[dateString substringWithRange:NSMakeRange(14, 2)] intValue];
        second = [[dateString substringWithRange:NSMakeRange(17, 2)] intValue];
        _timeStart= NO;
    }
    
    [endTime setYear:year];
    [endTime setMonth:month];
    [endTime setDay:day];
    [endTime setHour:hour];
    [endTime setMinute:minute];
    [endTime setSecond:second];
    NSDate *todate = [cal dateFromComponents:endTime]; //把目标时间装载入date
    //    [endTime release];
    
    //用来得到具体的时差，是为了统一成北京时间
    unsigned int unitFlags = NSCalendarUnitYear| NSCalendarUnitMonth| NSCalendarUnitDay| NSCalendarUnitHour| NSCalendarUnitMinute| NSCalendarUnitSecond;
    NSDateComponents *d = [cal components:unitFlags fromDate:today toDate:todate options:0];
    
    NSString *fen = [NSString stringWithFormat:@"%ld", (long)[d minute]];
    
    if([d minute] < 10) {
        fen = [NSString stringWithFormat:@"0%ld",(long)[d minute]];
        
    }
    NSString *miao = [NSString stringWithFormat:@"%ld", (long)[d second]];
    NSLog(@"%@",miao);
    if([d second] < 10) {
        miao = [NSString stringWithFormat:@"0%ld",(long)[d second]];
    }
    
    if([d second] > 0) {
        self.getcodeBtn.enabled = NO;
        [self.getcodeBtn setTitle:[NSString stringWithFormat:@"重新发送(%@)",miao] forState:UIControlStateDisabled];
        //        self.getcodeBtn.backgroundColor = [UIColor grayColor];
        //        倒计时的颜色
        //        [self.getcodeBtn setTitleColor:[UIColor colorWithRed:29/256.0 green:174/256.0 blue:253/256.0 alpha:1] forState:UIControlStateNormal];
        
    }else if([d second] <= 0) {
        
        [self.getcodeBtn setTitle:@"获取验证码" forState:UIControlStateDisabled];
        //        执行完成的颜色
        //        [self.getcodeBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        //        self.getcodeBtn.backgroundColor = [UIColor colorWithRed:29/256.0 green:174/256.0 blue:253/256.0 alpha:1];
        self.getcodeBtn.enabled = YES;
        
        [theTimer invalidate];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
